package com.gl.rent;


import cn.hutool.core.convert.Convert;
import com.gl.common.util.SecurityUtils;
import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.pojo.BusinessCircle;
import com.haixiaoke.saas.repository.service.IBusinessCircleService;
import com.haixiaoke.saas.repository.util.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;


/**
 * 商业区Controller
 *
 * @author haixiaoke
 * @date 2023-04-06
 */
@RestController
@RequestMapping("/base/businessCircle")
public class BusinessCircleController extends BaseController {
    @Autowired
    private IBusinessCircleService businessCircleService;

    /**
     * 查询商业区列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BusinessCircle businessCircle) {
        startPage();
        List<BusinessCircle> list = businessCircleService.selectBusinessCircleList(businessCircle);
        return getDataTable(list);
    }

    /**
     * 获取商业区详细信息
     */
    @GetMapping(value = "/{businessCircleId}")
    public AjaxResult getInfo(@PathVariable("businessCircleId") Long businessCircleId) {
        return success(businessCircleService.selectBusinessCircleByBusinessCircleId(businessCircleId));
    }

    /**
     * 新增商业区
     */
    @PostMapping
    public AjaxResult add(@Valid @RequestBody BusinessCircle businessCircle) {

        //校验商业区是否存在
        /*if (businessCircleService.checkLongitudeAndLatitude(businessCircle.getLongitude(), businessCircle.getLatitude()))
            return error("商业区已存在！");*/

        //填写创建人
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        businessCircle.setCreateBy(id);

        return toAjax(businessCircleService.insertBusinessCircle(businessCircle));
    }

    /**
     * 修改商业区
     */
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody BusinessCircle businessCircle) {

        //填写修改人
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        businessCircle.setUpdateBy(id);

        return toAjax(businessCircleService.updateBusinessCircle(businessCircle));
    }

    /**
     * 删除商业区
     */
    @DeleteMapping("/{businessCircleIds}")
    public AjaxResult remove(@PathVariable Long[] businessCircleIds) {
        return toAjax(businessCircleService.deleteBusinessCircleByBusinessCircleIds(businessCircleIds));
    }
}
